package cn.gemiman.daoyi.module.cms.controller.admin.appsitefootprint;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gemiman.daoyi.module.cms.controller.admin.appsitefootprint.vo.*;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsitefootprint.AppSiteFootprintDO;
import cn.gemiman.daoyi.module.cms.service.appsitefootprint.AppSiteFootprintService;

@Tag(name = "管理后台 - 站点足迹")
@RestController
@RequestMapping("/cms/app-site-footprint")
@Validated
public class AppSiteFootprintController {

    @Resource
    private AppSiteFootprintService appSiteFootprintService;

    @PostMapping("/create")
    @Operation(summary = "创建站点足迹")
    @PreAuthorize("@ss.hasPermission('cms:app-site-footprint:create')")
    public CommonResult<Long> createAppSiteFootprint(@Valid @RequestBody AppSiteFootprintSaveReqVO createReqVO) {
        return success(appSiteFootprintService.createAppSiteFootprint(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新站点足迹")
    @PreAuthorize("@ss.hasPermission('cms:app-site-footprint:update')")
    public CommonResult<Boolean> updateAppSiteFootprint(@Valid @RequestBody AppSiteFootprintSaveReqVO updateReqVO) {
        appSiteFootprintService.updateAppSiteFootprint(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除站点足迹")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:app-site-footprint:delete')")
    public CommonResult<Boolean> deleteAppSiteFootprint(@RequestParam("id") Long id) {
        appSiteFootprintService.deleteAppSiteFootprint(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得站点足迹")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:app-site-footprint:query')")
    public CommonResult<AppSiteFootprintRespVO> getAppSiteFootprint(@RequestParam("id") Long id) {
        AppSiteFootprintDO appSiteFootprint = appSiteFootprintService.getAppSiteFootprint(id);
        return success(BeanUtils.toBean(appSiteFootprint, AppSiteFootprintRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得站点足迹分页")
    @PreAuthorize("@ss.hasPermission('cms:app-site-footprint:query')")
    public CommonResult<PageResult<AppSiteFootprintRespVO>> getAppSiteFootprintPage(@Valid AppSiteFootprintPageReqVO pageReqVO) {
        PageResult<AppSiteFootprintDO> pageResult = appSiteFootprintService.getAppSiteFootprintPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppSiteFootprintRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出站点足迹 Excel")
    @PreAuthorize("@ss.hasPermission('cms:app-site-footprint:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAppSiteFootprintExcel(@Valid AppSiteFootprintPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppSiteFootprintDO> list = appSiteFootprintService.getAppSiteFootprintPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "站点足迹.xls", "数据", AppSiteFootprintRespVO.class,
                        BeanUtils.toBean(list, AppSiteFootprintRespVO.class));
    }

}